﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmCapNhatDMQuyen
    Private _objDMQ As DM_QUYEN
    Private _close As Boolean = False
    Public Property objDMQ() As DM_QUYEN
        Get
            Return _objDMQ
        End Get
        Set(ByVal value As DM_QUYEN)
            _objDMQ = value
        End Set
    End Property

    Private _ActionForm As String
    Public Property ActionForm() As String
        Get
            Return _ActionForm
        End Get
        Set(ByVal value As String)
            _ActionForm = value
        End Set
    End Property

    Private _IDQuyen As String = ""
    Public Property IDQuyen() As String
        Get
            Return _IDQuyen
        End Get
        Set(ByVal value As String)
            _IDQuyen = value
        End Set
    End Property

    Private Function CheckSave() As String
        Dim vError As String = ""
        If Trim(TxtMaQuyen.Text) = "" Then
            vError = "Bạn chưa nhập mã quyền"
            TxtMaQuyen.Focus()
        ElseIf Trim(TxtTenQuyen.Text) = "" Then
            vError = "Bạn chưa nhập tên quyền"
            TxtTenQuyen.Focus()
        ElseIf Trim(TxtThuTu.Text) = "" Then
            vError = "Bạn chưa nhập thứ tự"
            TxtThuTu.Focus()
        End If
        If ActionForm = vAddAction Then
            Dim vMa As String = (From ds In dbTV.DM_Quyens _
                                Where ds.InUsed And ds.MaQuyen.Equals(Trim(TxtMaQuyen.Text)) _
                                Select ds.MaQuyen).FirstOrDefault
            If vMa IsNot Nothing Then
                vError = "Đã tồn tại mã quyền"
                TxtMaQuyen.Focus()
            End If
        End If

        Return vError
    End Function

    Private Sub FrmCapNhatDMQuyen_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub FrmCapNhatDMQuyen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.btnHuyBo.Caption = BtnCancelCaption
        Me.btnGhiVaDong.Caption = BtnSaveAndCloseCaption
        Me.btnTroGiup.Caption = BtnHelpCaption
        Me.Text = "Cập nhật danh mục quyền"
        Select Case ActionForm
            Case vAddAction
            Case vEditAction, vCopyAction
                If objDMQ IsNot Nothing Then
                    Me.TxtMaQuyen.Text = objDMQ.MaQuyen
                    Me.TxtTenQuyen.Text = objDMQ.TenQuyen
                    Me.TxtThuTu.Text = CStr(objDMQ.ThuTu)
                    Me.TxtMaQuyen.Properties.ReadOnly = True
                    Me.TxtMaQuyen.TabStop = False
                    Dim Ten_QCha As DM_Quyen = (From ds In dbTV.DM_Quyens _
                                              Where ds.InUsed And ds.ID = objDMQ.ParentID _
                                              Select ds).FirstOrDefault
                    If Ten_QCha IsNot Nothing Then
                        txtMaQuyenCha.Text = Ten_QCha.MaQuyen
                        txtMaQuyenCha.Tag = Ten_QCha.ID
                        txtTenQuyenCha.Text = Ten_QCha.TenQuyen
                    End If
                End If
                If ActionForm = vCopyAction Then
                    ActionForm = vAddAction
                    Me.TxtMaQuyen.Properties.ReadOnly = False
                    Me.TxtMaQuyen.TabStop = True
                End If
        End Select
    End Sub

    Private Sub btnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaDong.ItemClick
        Dim vCheck As String = CheckSave()
        If vCheck <> "" Then
            MsgErr(vCheck)
            _close = False
            Exit Sub
        End If
        Try
            Select Case ActionForm
                Case vAddAction
                    Dim vAddPermission As New DM_Quyen
                    vAddPermission.ID = CreateID()
                    IDQuyen = vAddPermission.ID
                    vAddPermission.MaQuyen = Trim(Me.TxtMaQuyen.Text)
                    vAddPermission.TenQuyen = Trim(Me.TxtTenQuyen.Text)
                    vAddPermission.ParentID = CStr(txtMaQuyenCha.Tag)
                    vAddPermission.ThuTu = CInt(Me.TxtThuTu.Text)
                    vAddPermission.InUsed = True
                    'vAddPermission.IsSent = False
                    vAddPermission.EditedOn = CDate(dbTV.GetDateTimeServer)

                    dbTV.DM_Quyens.InsertOnSubmit(vAddPermission)
                Case vEditAction
                    Dim vEditPermission As DM_Quyen = (From ds In dbTV.DM_Quyens _
                                                        Where ds.ID = objDMQ.ID _
                                                        Select ds).FirstOrDefault()
                    If vEditPermission IsNot Nothing Then
                        IDQuyen = vEditPermission.ID
                        vEditPermission.MaQuyen = Trim(Me.TxtMaQuyen.Text)
                        vEditPermission.TenQuyen = Trim(Me.TxtTenQuyen.Text)
                        vEditPermission.ThuTu = CInt(Me.TxtThuTu.Text)
                        vEditPermission.ParentID = CStr(txtMaQuyenCha.Tag)
                        vEditPermission.EditedOn = CDate(dbTV.GetDateTimeServer())
                    End If
            End Select
            iSubmitChange()
            ActionForm = vUpdateAction
            _close = True
            Me.Close()
        Catch ex As Exception
            _close = False
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub btnHuyBo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnHuyBo.ItemClick
        Me.Close()
    End Sub

    Private Sub btnTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTroGiup.ItemClick
        vMsg.Show("Help")
    End Sub

    ''' <summary>
    ''' Phím tắt
    ''' @NhiLV
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub FrmCapNhatDMQuyen_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Thoát (Huỷ bỏ)
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
            e.KeyCode = GetKeyCode2(pIdThoat)) Or _
            (e.KeyCode = GetKeyCode(pIdThoat)) Then

            btnHuyBo_ItemClick(Nothing, Nothing)
        End If
        'Ghi và đóng
        If (e.Modifiers = GetKeyModifide(pIdGhiVaDong) And _
            e.KeyCode = GetKeyCode2(pIdGhiVaDong)) Or _
            (e.KeyCode = GetKeyCode(pIdGhiVaDong)) Then

            If btnGhiVaDong.Enabled = True And Me.btnGhiVaDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                btnGhiVaDong_ItemClick(Nothing, Nothing)
            End If
        End If
        'Trợ giúp
        If (e.Modifiers = GetKeyModifide(pIdTroGiup) And _
            e.KeyCode = GetKeyCode2(pIdTroGiup)) Or _
            (e.KeyCode = GetKeyCode(pIdTroGiup)) Then

            If Me.btnTroGiup.Enabled = True And Me.btnTroGiup.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                btnTroGiup_ItemClick(Nothing, Nothing)
            End If
        End If
    End Sub

    Private Sub txtMaQuyenCha_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaQuyenCha.Leave
        Dim objQ = (From ds In dbTV.DM_Quyens Where ds.InUsed And ds.MaQuyen = txtMaQuyenCha.Text.Trim Select ds).FirstOrDefault
        If objQ IsNot Nothing Then
            txtMaQuyenCha.Text = objQ.MaQuyen
            txtMaQuyenCha.Tag = objQ.ID
            txtTenQuyenCha.Text = objQ.TenQuyen
        Else
            Dim frm As New FrmDialogDMQ
            frm.ShowDialog()
            objQ = frm.vPermission
            If objQ IsNot Nothing Then
                txtMaQuyenCha.Text = objQ.MaQuyen
                txtMaQuyenCha.Tag = objQ.ID
                txtTenQuyenCha.Text = objQ.TenQuyen
            Else
                txtMaQuyenCha.Text = ""
                txtMaQuyenCha.Tag = Nothing
                txtTenQuyenCha.Text = ""
            End If
        End If
    End Sub
End Class